﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Data;
using Microsoft.Phone.Controls;
using MusicAlarm7.Resources;

namespace MusicAlarm7.DataModel
{
    public class RecurringDaysConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string str = value.ToString();
            List<object> list = new List<object>();

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '1')
                {
                    DateTime d = new DateTime(2013, 4, 1);
                    while ((int)d.DayOfWeek != i)
                    {
                        d = d.AddDays(1);
                    }
                    list.Add(d.ToString("dddd"));
                }
            }

            return list;
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            char[] carr = "0000000".ToCharArray();
            IList list = (IList)value;

            foreach (object s in list)
            {
                carr[(int)AlarmNotification.ParseDayOfWeek(s.ToString())] = '1';
            }

            return new string(carr);
        }
    }

    public class RecurrenceSummaryConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            RecurringDaysConverter str2list = new RecurringDaysConverter();
            IList list = (IList)str2list.Convert(value, typeof(IList), parameter, culture);
            RecurringDaysPicker ctrl = new RecurringDaysPicker();
            ctrl.Language = System.Windows.Markup.XmlLanguage.GetLanguage("zh-CN");
            return AppResources.MainPage_Recurrence + ctrl.SummaryForSelectedItemsDelegate(list);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class EtaTimeSpanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TimeSpan eta = (TimeSpan)value;
            if (eta == TimeSpan.Zero)
            {
                return AppResources.MainPage_Eta + AppResources.MainPage_Expired;
            }
            else if (Math.Floor(eta.TotalMinutes) <= 0)
            {
                return AppResources.MainPage_Eta + AppResources.MainPage_RightNow;
            }
            else
                return AppResources.MainPage_Eta + (eta.Days == 0 ? "" : eta.ToString("%d") + AppResources.MainPage_Eta_Days) + eta.ToString(@"hh\:mm") + AppResources.MainPage_Eta_Minutes;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}